### Project 4: Breathing LED ![](media/25107e92a36e701f271b2371359f2679.jpeg) **Overview** A“breathing LED”is a phenomenon where an LED's brightness smoothly changes from dark to bright and back to dark, continuing to do so and giving the illusion of an LED“breathing. This phenomenon is similar to a lung breathing in and out. So how to control LED’s brightness? We need to take advantage of PWM. **Components** ![image-20231020083030398](media/image-20231020083030398.png) **Connection Diagram** ![](media/fed849dd5952f3b94a591d5bc5e64267.png) **Test Code** ```c //********************************************************************** /* * Filename : Breathing Led * Description : Make led light fade in and out, just like breathing. * Auther : http//www.keyestudio.com */ #define PIN_LED 0 //define the led pin #define CHN 0 //define the pwm channel #define FRQ 1000 //define the pwm frequency #define PWM_BIT 8 //define the pwm precision void setup() { ledcSetup(CHN, FRQ, PWM_BIT); //setup pwm channel ledcAttachPin(PIN_LED, CHN); //attach the led pin to pwm channel } void loop() { for (int i = 0; i < 255; i++) { //make light fade in ledcWrite(CHN, i); delay(10); } for (int i = 255; i > -1; i--) { //make light fade out ledcWrite(CHN, i); delay(10); } } //************************************************************************************* ``` **Test Result** Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on,then the LED on the module gradually gets dimmer then brighter, cyclically, like human breathe.